﻿using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Path = System.IO.Path;

namespace RCCreateMesh
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectImagePatch_Btn_Click(object sender, RoutedEventArgs e)
        {
            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            dialog.IsFolderPicker = true;
            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                SelectImagePatch_TBox.Text = dialog.FileName;
            }
        }

        /// <summary>
        /// 选择导出Mesh的路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OutMeshPatch_Btn2_Click(object sender, RoutedEventArgs e)
        {
            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            dialog.IsFolderPicker = true;
            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                OutMeshPatch_TBox2.Text = dialog.FileName;
            }
        }

        /// <summary>
        /// 选择保存工程的路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OutSaveProjectPatch_Btn_Click(object sender, RoutedEventArgs e)
        {
            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            dialog.IsFolderPicker = true;
            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                OutSaveProjectPatch_TBox.Text = dialog.FileName;
            }
        }

        private void Exe_Btn_Click(object sender, RoutedEventArgs e)
        {
            string selectImagePatch = SelectImagePatch_TBox.Text;
            string outMeshPatch = OutMeshPatch_TBox2.Text;
            string outProjectsPatch = OutSaveProjectPatch_TBox.Text;

            if (IsNullOrEmptyPatch(selectImagePatch) || IsNullOrEmptyPatch(outMeshPatch) || IsNullOrEmptyPatch(outProjectsPatch))
            {
                Info_textBlock.Text = "请填写正确的路径";
                return;
            }

            Process p = new Process();
            //设置要启动的应用程序
            p.StartInfo.FileName = "cmd.exe";
            //是否使用操作系统shell启动
            p.StartInfo.UseShellExecute = false;
            // 接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardInput = true;
            //输出信息
            p.StartInfo.RedirectStandardOutput = true;
            // 输出错误
            p.StartInfo.RedirectStandardError = true;
            //不显示程序窗口
            p.StartInfo.CreateNoWindow = true;
            //启动程序
            p.Start();

            string basePatch = System.AppDomain.CurrentDomain.BaseDirectory; //运行目录
            string RCPatch = @"C:\Program Files\Capturing Reality\RealityCapture";
            string RCExe = "RealityCapture.exe";

            string fileDriveString = RCPatch.Substring(0, RCPatch.IndexOf(":") + 1); //盘符位置
            p.StandardInput.WriteLine(fileDriveString);
            p.StandardInput.WriteLine("cd " + RCPatch); // CD 到目录

            StringBuilder sbCMD = new StringBuilder(RCExe); //cmd 命令
            DirectoryInfo imageFolder = new DirectoryInfo(selectImagePatch); //图片文件目录

            foreach (var item in imageFolder.GetDirectories())
            {
                sbCMD.Append(" -newScene") // 新场景
                    .Append(" -addFolder") // 添加图片文件夹
                    .Append(" " + item.FullName)
                    .Append(" -align") //对齐
                    //.Append(" -mvsPreview") //生成预览模型
                    .Append(" -mvsHigh") //生成模型
                    .Append(" -unwrap")  //展UV
                    .Append(" -calculateTexture") // 计算纹理
                    .Append(" -calculateVertexColors") //计算顶点颜色
                    .Append(" -exportModel \"Model 1\"") //导出模型设置
                    .Append(" " + CreateDir(Path.Combine(outMeshPatch, item.Name))+ "\\"+item.Name + ".obj") //保存mesh路径
                    .Append(" " + Path.Combine(basePatch,"pp.xml")) //保存mesh配置文件
                    .Append(" -save")  // 保存工程
                    .Append(" " + CreateDir(Path.Combine(outProjectsPatch, item.Name)) + "\\" + item.Name + ".rcproj")
                    ;
            }
            sbCMD.Append(" -quit"); //退出程序
            //向cmd窗口发送输入信息
            p.StandardInput.WriteLine(sbCMD.ToString() + " &exit");
            p.StandardInput.AutoFlush = true;
            //获取输出信息
            string strOuput = p.StandardOutput.ReadToEnd();
            //等待程序执行完退出进程
            p.WaitForExit();
            p.Close();
            Info_textBlock.Text = strOuput;
        }

        /// <summary>
        /// 是否设置了路径
        /// </summary>
        /// <param name="patch"></param>
        /// <returns></returns>
        private bool IsNullOrEmptyPatch(string patch)
        {
            return string.IsNullOrEmpty(patch);
        }

        private string CreateDir(string dirName)
        {
           return Directory.CreateDirectory(dirName).FullName;
        }
    }
}
